<template>
  <div class="character">
    <div class="main">
      <div class="allsearch">
        <section class="search-content">
          <el-row style="width:100%;">
            <el-col :span="14">
              <el-input
                v-model="listQuery.name"
                style="width:40%;"
                placeholder="关键词"
                @keyup.enter.native="resourceHandleFilter"
              />
              <el-button type="primary" size="medium" @click="resourceHandleFilter">搜索</el-button>
              <el-button size="medium" type="info" @click="clearParams">清空</el-button>
            </el-col>
            <el-col :span="10">
              <div style="width:100%">
                <el-button
                  type="primary"
                  size="medium"
                  style="float:right;"
                  @click="addCLick()"
                >新增
                </el-button>
              </div>
            </el-col>
          </el-row>
        </section>
      </div>
      <el-dialog :visible.sync="addDialog" title="新增" width="50%">
        <div class="posterBox">
          <el-form :model="OrganizationUnit" label-width="150px" ref="organizationForm">
            <el-form-item label="名称">
              <el-input v-model="OrganizationUnit.fs" placeholder="名称" style="width:300px"/>
            </el-form-item>
            <el-form-item label="全称">
              <el-input v-model="OrganizationUnit.fullName" placeholder="全称" style="width:300px"/>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
        <el-button @click="addDialog = false">取 消</el-button>
        <el-button type="primary" @click="createData()">保 存</el-button>
      </span>
      </el-dialog>

      <!-- 删除 -->
      <el-dialog :visible.sync="handleDelete" width="25%">
        <div style="width:100%;float:left;height:42px;">确定要删除吗?</div>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="deleteData(deleteRow)">确定</el-button>
          <el-button @click="handleDelete = false">取消</el-button>
        </div>
      </el-dialog>
      <!-- 删除 -->

      <el-table
        :table-label="tableLabel"
        :table-data="listFilter.slice((currentpage-1)*pagesize,currentpage*pagesize)"
        :list-loading="listLoading"
      >
        <el-table-column slot="button" :min-width="1" label="操作" prop="manage" align="center">
          <template slot-scope="scope">
            <el-button @click="updateCLick(scope.row)" type="primary" title='修改' icon="el-icon-edit" circle></el-button>
            <el-button @click="deleteCLick(scope.row)" type="danger" title='删除' icon="el-icon-delete" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :total="total"
        :current-page="currentpage"
        :page-size="pagesize"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
  import ElTable from '@/components/Eltable'
  import {scrollTo} from '@/utils/scrollTo' // 上下滑动组件
  import Pagination from '@/components/Pagination' // 分页组件
  import {add, deleteOrganizationUnit, list} from "@/api/system/org"; // 数据接口
  import {getTenantId} from '@/utils/auth';

  export default {
    components: {
      ElTable,
      Pagination
    },
    data() {
      return {
        tenantId: "",
        OrganizationUnit: {
          fs: '1',
          fullName: '',
          tenantId: '',
        },
        formLabelWidth: '135px',
        addDialog: false,//新增
        handleEdit: false,//编辑
        handleDelete: false,//删除
        deleteRow: '',
        createRow: '',
        deleteCLick(row) {
          this.handleDelete = true;
          this.deleteRow = row;
        },
        // 列表编辑按钮
        addCLick() {
          this.addDialog = true;
          this.OrganizationUnit = {}
        },
        // 列表编辑按钮
        updateCLick(row) {
          this.addDialog = true;
          this.OrganizationUnit = Object.assign({}, row)
        },
        total: 0,
        currentpage: 1,
        pagesize: 10,
        tableLabel: [{
            label: '名称', width: '2', prop: 'fs'
          }, {
            label: '全称', width: '2', prop: 'fullName'
          }, {
            label: '创建时间', width: '2', prop: 'createTimeTimeStr'
          }
        ],
        listFilter: [],
        listLoading: false,
        listQuery: {
          name: '',
        }
      }
    },
    mounted() {
      this.getData();
    },
    methods: {
      //接口获取数据
      getData() {
        this.listLoading = true;
        list(getTenantId()).then(response => {
          this.total = response.data.items.length;
          this.listFilter = response.data.items;
          this.listLoading = false
        });
      },
      // 数据检索
      resourceHandleFilter() {
        this.getData()
      },
      clearParams() {
        this.listQuery.name = '';
        this.getData();
      },

      handleSizeChange(val) {
        this.currentpage = 1;
        this.pagesize = val.value;
        if (this.pagesize >= 20) {
          scrollTo(50, 800)
        }
      },
      handleCurrentChange(val) {
        this.currentpage = val.value;
        if (this.pagesize >= 20) {
          scrollTo(50, 800)
        }
      },
      createData() {
        this.$refs['organizationForm'].validate((valid) => {
          if (valid) {
            this.OrganizationUnit.tenantId = getTenantId();
            add(this.OrganizationUnit).then(() => {
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              });
              setTimeout(function () {
              }, 150);
              this.getData();
              this.$refs['organizationForm'].resetFields();
            })
          }
          this.addDialog = false
        })
      },
      deleteData(row) {
        this.handleDelete = false;
        deleteOrganizationUnit(row.id).then(() => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          });
          this.getData();
        })
      }
    }
  }
</script>

<style lang="scss">
  .character {
    .main {
      padding: 0 20px;
    }

    .allsearch {
      margin-top: 14px;
      padding: 0;

      .search-content {
        width: 100%;
      }
    }

    .el-dialog {
      border-radius: 5px;
      overflow: hidden;
    }
  }
</style>
